layui.use(['table', 'admin', 'ax','element','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
	var element = layui.element
    var form = layui.form;
	
	//Tab switch
	element.on('tab(doTabBrief)',function(data){
		console.log(data.index)
	})

    /**
     * 空闲车
     */
    var BasicPrivatecar = {
        tableId: "basicPrivatecarTable"
    };

    /**
     * 已调车
     */
    /*var BasicPrivatecar2 = {
        tableId: "basicPrivatecarTable2"
    };*/

    /**
     * 在途车
     */
    var BasicPrivatecar3 = {
        tableId: "basicPrivatecarTable3"
    };

    /**
     * Initialize table columns
     */
    BasicPrivatecar.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'ID'},
            {field: 'privatecarState', title: 'Status',templet: '#billstateTpl'},
            {field: 'operateName', title: 'Registered Project', style:'font-weight: 600;'},
            {field: 'privatecarFleetName', title: 'Carrier', style:'font-weight: 600;'},
            {field: 'privatecarCarnumber', title: 'License Plate', style:'font-weight: 600;'},

            {field: 'driver1', title: 'Driver', style:'font-weight: 600;'},
            {field: 'tel1', title: 'Contact Info', style:'font-weight: 600;'},

            {field: 'privatecarSurevolume', title: 'Allowed Volume m³', style:'font-weight: 600;', sort: true},
            {field: 'privatecarColour', title: 'Plate Color', style:'font-weight: 600;'},
            {field: 'privatecarType', title: 'Vehicle Type', style:'font-weight: 600;'},
            {align: 'center', toolbar: '#tableBar', title: 'Operation' ,fixed: 'right'}
        ]];
    };

    /**
     * 初始化表格的列
     */
    /*BasicPrivatecar2.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: '主键'},
            {field: 'privatecarState', title: '状态',templet: '#billstateTpl'},
            {field: 'privatecarCarnumber', title: '车牌号', style:'font-weight: 600;'},

            {field: 'driver1', title: '司机', style:'font-weight: 600;'},
            {field: 'tel1', title: '联系电话', style:'font-weight: 600;'},

            {field: 'privatecarColour', title: '车牌颜色', style:'font-weight: 600;'},
            {field: 'privatecarType', title: '车型', style:'font-weight: 600;'},
            {field: 'privatecarSurevolume', title: '准载体积m3', style:'font-weight: 600;', sort: true},
            {align: 'center', toolbar: '#tableBar2', title: '操作' ,fixed: 'right',width:150}
        ]];
    };*/

    /**
     * Initialize table columns
     */
    BasicPrivatecar3.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'ID'},
            {field: 'privatecarState', title: 'Status',templet: '#billstateTpl'},
            {field: 'operateName', title: 'Registered Project', style:'font-weight: 600;'},
            {field: 'privatecarFleetName', title: 'Carrier', style:'font-weight: 600;'},
            {field: 'privatecarCarnumber', title: 'License Plate', style:'font-weight: 600;'},

            {field: 'driver1', title: 'Driver', style:'font-weight: 600;'},
            {field: 'tel1', title: 'Contact Info', style:'font-weight: 600;'},

            {field: 'privatecarSurevolume', title: 'Allowed Volume m³', style:'font-weight: 600;', sort: true},
            {field: 'privatecarColour', title: 'Plate Color', style:'font-weight: 600;'},
            {field: 'privatecarType', title: 'Vehicle Type', style:'font-weight: 600;'},
            {align: 'center', toolbar: '#tableBar3', title: 'Operation' ,fixed: 'right'}
        ]];
    };

    /**
     * Click search button
     */
    BasicPrivatecar.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['privatecarFleetId'] = $("#privatecarFleetId").val();
        queryData['condition'] = $("#condition").val();
        table.reload(BasicPrivatecar.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 点击查询按钮
     */
    /*BasicPrivatecar2.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition2").val();
        queryData['driver1'] = $("#driver1_2").val();
        table.reload(BasicPrivatecar2.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };*/

    /**
     * 点击查询按钮
     */
    BasicPrivatecar3.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId3").val();
        queryData['privatecarFleetId'] = $("#privatecarFleetId3").val();
        queryData['condition'] = $("#condition3").val();
        table.reload(BasicPrivatecar3.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // Render table
    var tableResult = table.render({
        elem: '#' + BasicPrivatecar.tableId,
        url: Feng.ctxPath + '/orderTransportDispatch/privatecarlist?privatecarState=Idle',
        page: true,
        limit: 40,
        height: "full-60",
        cellMinWidth: 100,
        cols: BasicPrivatecar.initColumn()
    });

    // 渲染表格
    /*var tableResult2 = table.render({
        elem: '#' + BasicPrivatecar2.tableId,
        url: Feng.ctxPath + '/orderTransportDispatch/privatecarlist?privatecarState=yidiao',
        page: true,
        limit: 40,
        height: "full-60",
        cellMinWidth: 100,
        cols: BasicPrivatecar2.initColumn()
    });*/

    // 渲染表格
    var tableResult3 = table.render({
        elem: '#' + BasicPrivatecar3.tableId,
        url: Feng.ctxPath + '/orderTransportDispatch/privatecarlist?privatecarState=yunshu',
        page: true,
        limit: 40,
        height: "full-60",
        cellMinWidth: 100,
        cols: BasicPrivatecar3.initColumn()
    });

    // Search button click event
    $('#btnSearch').click(function () {
        BasicPrivatecar.search();
    });

    // 搜索按钮点击事件
    /*$('#btnSearch2').click(function () {
        BasicPrivatecar2.search();
    });*/

    // 搜索按钮点击事件
    $('#btnSearch3').click(function () {
        BasicPrivatecar3.search();
    });

    //Query warehouse area
    form.on('select(operateId)',function(){
        $("#privatecarFleetId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicCarrier/Querylist',
            dataType:"json",   //Return format as json
            async: false,
            data:{"operateId":$("#operateId").val()},    //Parameter value
            type:"post",   //Request method
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>Select Carrier</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.carrierName+"</option>";
                    });
                    $("#privatecarFleetId").html(str);
                }
            }
        });
        form.render('select');
    });

    //Query warehouse area
    form.on('select(operateId3)',function(){
        $("#privatecarFleetId3").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicCarrier/Querylist',
            dataType:"json",   //Return format as json
            async: false,
            data:{"operateId":$("#operateId3").val()},    //Parameter value
            type:"post",   //Request method
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>Select Carrier</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.carrierName+"</option>";
                    });
                    $("#privatecarFleetId3").html(str);
                }
            }
        });
        form.render('select');
    });

    // Toolbar click event
    table.on('tool(' + BasicPrivatecar.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'xianze') {
			let gids = []
			let bids = []
			let nums = []
			let orders = []
			let arrorders = []
            for (var i = 0; i < parent.list2.length; i++) {
				if(parent.list2[i].goodId != ''){
					gids.push(parent.list2[i].goodId)
					bids.push(parent.list2[i].batchnumber)
					nums.push(parent.list2[i].num)
					orders.push(parent.list2[i].transportNumber)
				} else {
					arrorders.push(parent.list2[i].transportNumber)
				}
			}
            layer.confirm('Confirm to select this vehicle?', function(index){
            	layer.close(index);
            	var msg;
                $.ajax({
                	url:Feng.ctxPath + "/orderTransportDispatch/batchassign?gids="+gids+"&bids="+bids+"&nums="+nums+"&orders="+orders+"&arrorders="+arrorders,
					data:{carId:data.id},
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                		msg = layer.msg('Submitting... Please wait patiently', {
                			  icon: 16,
                			  shade: [0.2,'#000'], //0.1 opacity white background
                			  time: false
                		});
                	},
                	success:function(data){
                    	layer.close(msg); 
                		if (data.success) {
							parent.submit = 1
			            	Feng.success("Submit successful!");
					    	var Index = parent.layer.getFrameIndex(window.name);
					        parent.layer.close(Index);
						} else {
  		                    Feng.error("Submit failed！"+data.message);
						}
                	},
                	error:function(data){
                    	layer.close(msg); 
                        Feng.error("提交失败！" + data.responseJSON.message)
                	}
                })
            }); 
        } 
    });

    // 工具条点击事件
    /*table.on('tool(' + BasicPrivatecar2.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'xianze2') {
			let gids = []
			let nums = []
			let orders = []
			let arrorders = []
            for (var i = 0; i < parent.list2.length; i++) {
				if(parent.list2[i].goodId != ''){
					gids.push(parent.list2[i].goodId)
					nums.push(parent.list2[i].num)
					orders.push(parent.list2[i].transportNumber)
				} else {
					arrorders.push(parent.list2[i].transportNumber)
				}
			}
            layer.confirm('确定选择该车辆吗？', function(index){
            	layer.close(index);
            	var msg;
                $.ajax({
                	url:Feng.ctxPath + "/orderTransportDispatch/batchassign?gids="+gids+"&nums="+nums+"&orders="+orders+"&arrorders="+arrorders,
					data:{carnumbger:data.privatecarCarnumber},
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                		msg = layer.msg('提交中。。。请耐心等待', {
                			  icon: 16,
                			  shade: [0.2,'#000'], //0.1透明度的白色背景
                			  time: false
                		});
                	},
                	success:function(data){
                    	layer.close(msg); 
                		if (data.success) {
							parent.submit = 1
                        	Feng.success("提交成功!");
					    	var Index = parent.layer.getFrameIndex(window.name);
					        parent.layer.close(Index);
						} else {
  		                    Feng.error("提交失败！"+data.message);
						}
                	},
                	error:function(data){
                    	layer.close(msg); 
                        Feng.error("提交失败！" + data.responseJSON.message)
                	}
                })
            }); 
        } else if (layEvent === 'tomingxi'){
            layer.open({
                type: 2,
                title: '查询已配置明细',
                area: ['900px', '480px'],
                anim: 5,
                content: Feng.ctxPath + '/orderTransportDispatch/tochecktoo?carnumbger=' + data.privatecarCarnumber,
            });
		}
    });*/

    // 工具条点击事件
    table.on('tool(' + BasicPrivatecar3.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'xianze3') {
			let gids = []
			let bids = []
			let nums = []
			let orders = []
			let arrorders = []
            for (var i = 0; i < parent.list2.length; i++) {
				if(parent.list2[i].goodId != ''){
					gids.push(parent.list2[i].goodId)
					bids.push(parent.list2[i].batchnumber)
					nums.push(parent.list2[i].num)
					orders.push(parent.list2[i].transportNumber)
				} else {
					arrorders.push(parent.list2[i].transportNumber)
				}
			}
            layer.confirm('确定选择该车辆吗？', function(index){
            	layer.close(index);
            	var msg;
                $.ajax({
                	url:Feng.ctxPath + "/orderTransportDispatch/batchassign?gids="+gids+"&bids="+bids+"&nums="+nums+"&orders="+orders+"&arrorders="+arrorders,
					data:{carId:data.id},
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                		msg = layer.msg('提交中。。。请耐心等待', {
                			  icon: 16,
                			  shade: [0.2,'#000'], //0.1透明度的白色背景
                			  time: false
                		});
                	},
                	success:function(data){
                    	layer.close(msg); 
                		if (data.success) {
							parent.submit = 1
                        	Feng.success("提交成功!");
					    	var Index = parent.layer.getFrameIndex(window.name);
					        parent.layer.close(Index);
						} else {
  		                    Feng.error("提交失败！"+data.message);
						}
                	},
                	error:function(data){
                    	layer.close(msg); 
                        Feng.error("提交失败！" + data.responseJSON.message)
                	}
                })
            }); 
        } 
    });
});
